﻿/**
 * @class List: 列表控件
 * Ext.ux.List
 **/
Ext.ux.List = function(config){
    Ext.ux.List.superclass.constructor.call(this, Ext.apply(config, {
        data : config.data
    }));
};

Ext.extend(Ext.ux.List, Ext.grid.GridPanel, {
    hideHeaders         : true,
    stripeRows          : false,
    iconCls             : 'icon-grid',
    autoExpandColumn    : 'label',
    activeItem          : 0,
    viewConfig          : {forceFit : true},

    initComponent : function() {
        this.store = new Ext.data.SimpleStore({
            fields : [{name : 'label'}, {name : 'value'}]
        });

        if(this.data != null){
            this.store.loadData(this.data);
        }

        this.cm = new Ext.grid.ColumnModel([{
            id          : 'label',
            width       : this.listWidth,
            dataIndex   : 'label',
            sortable    : true
        }]);

        Ext.ux.List.superclass.initComponent.call(this);
    }
});
Ext.reg('ext-ux-list', Ext.ux.List);

//Ext.grid.RadioColumn
Ext.grid.RadioColumn = function(config){
    Ext.apply(this, config);
    if(!this.id){
        this.id = Ext.id();
    }
    this.renderer = this.renderer.createDelegate(this);
};

Ext.grid.RadioColumn.prototype = {
    init : function(grid){
        this.grid = grid;
        this.grid.on('render', function(){
            var view = this.grid.getView();
            view.mainBody.on('mousedown', this.onMouseDown, this);
        }, this);
    },

    onMouseDown : function(e, t){
        if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
            e.stopEvent();
            var index = this.grid.getView().findRowIndex(t);
            for(i=0;i<this.grid.store.getCount();i++){
            var record = this.grid.store.getAt(i);
                if(i==index){
                    record.set(this.dataIndex, true);
                }else{

                    record.set(this.dataIndex, false);
                }
            }
        }
    },

    renderer : function(v, p, record){
        p.css += ' x-grid3-radio-col-td';
        return '<div class="x-grid3-radio-col'+(v == true?'-on':'')+' x-grid3-cc-'+this.id+'"> </div>';
    }
};
Ext.reg('ext-ux-grid-radiocolumn',Ext.grid.RadioColumn);

//Ext.grid.CheckColumn
Ext.grid.CheckColumn = function(config){
    Ext.apply(this, config);
    if(!this.id){
        this.id = Ext.id();
    }
    this.renderer = this.renderer.createDelegate(this);
};

Ext.grid.CheckColumn.prototype ={
    init : function(grid){
        this.grid = grid;
        this.grid.on('render', function(){
            var view = this.grid.getView();
            view.mainBody.on('mousedown', this.onMouseDown, this);
        }, this);
    },

    onMouseDown : function(e, t){
        if(t.className && t.className.indexOf('x-grid3-cc-'+this.id) != -1){
            e.stopEvent();
            var index = this.grid.getView().findRowIndex(t);
            var record = this.grid.store.getAt(index);
            record.set(this.dataIndex, !record.data[this.dataIndex]);
        }
    },

    renderer : function(v, p, record){
        p.css += ' x-grid3-check-col-td';
        return '<div class="x-grid3-check-col'+(v?'-on':'')+' x-grid3-cc-'+this.id+'">&#160;</div>';
    }
};
Ext.reg('ext-ux-grid-checkcolumn',Ext.grid.RadioColumn);

Ext.override(Ext.form.RadioGroup, {
    afterRender: function() {
        var group = this;
        this.items.each(function(field) {
            // Listen for 'check' event on each child item
            field.on("check", function(self, checked) {

              // if checkbox is checked, then fire 'change' event on RadioGroup container
              if(checked)
                // Note, oldValue (third parameter in 'change' event listener) is not passed,
                // because is not easy to get it
                group.fireEvent('change', group, self.getRawValue());

            });
        });

        Ext.form.RadioGroup.superclass.afterRender.call(this)
    }
});